<template>
  <div class="wrapper">
    <div class="goods">
      <!-- 顶部过滤器 -->
      <div class="goods__filter">
        <ul class="category">
          <li class="title">分类:</li>
          <li
            :class="[
              'category__item item',
              { active: searchParams.categoryId === c.id },
            ]"
            v-for="(c, i) in category"
            :key="i"
            @click="setFilter({ category: c.id })"
          >
            {{ c.name }}
          </li>
        </ul>
        <div class="divider"></div>
        <ul class="sort">
          <li class="title">排序:</li>
          <li class="sort__item item">默认</li>
          <li
            class="sort__item item"
            v-for="(s, i) in sortKey"
            :key="i"
            @click="setFilter({ sortId: i })"
          >
            {{ s.label }}
          </li>
        </ul>
      </div>
      <!-- 商品列表 -->
      <div class="goods__list">
        <goods-list
          :list="goods"
          :page.sync="pageOptions.page"
          :size="pageOptions.size"
          :total="pageOptions.total"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  defineComponent,
  onMounted,
  reactive,
  ref,
  watch,
} from '@vue/composition-api';
import { getProduct, getCategory } from '../../api/user';
import GoodsList from '../../components/GoodsList.vue';

const sortKey = [
  { key: 'count', label: '销量' },
  { key: 'price', label: '价格' },
];

export default defineComponent({
  components: { GoodsList },
  setup() {
    const goods = ref([]);
    const category = ref([]);

    const pageOptions = reactive({
      page: 1,
      size: 50,
      total: 0,
    });

    const getCategories = async () => {
      await getCategory()
        .then((response) => {
          category.value = response.data.data;
          console.log('类别');
          console.log(category.value);
        })
        .catch((error) => {
          console.log(error);
        });
    };
    const searchParams = reactive({
      categoryId: '',
      shopId: '',
      name: '',
    });

    const getGoods = async () => {
      const { code, data } = await getProduct(
        pageOptions.page,
        pageOptions.size,
        searchParams
      );
      if (code === 100) {
        goods.value = data.products;
        pageOptions.total = data.total;
      }
    };

    onMounted(() => {
      getCategories();
      getGoods();
    });

    const setFilter = ({ category }) => {
      typeof category !== 'undefined' && (searchParams.categoryId = category);
      getGoods();
    };

    watch(pageOptions.page, () => {
      getGoods();
    });

    return {
      category,
      setFilter,
      sortKey,
      searchParams,
      goods,
      pageOptions,
    };
  },
});
</script>

<style lang="less" scoped>
.wrapper {
  padding: 40px 0;
  background: #f9f9f9;
  .goods {
    width: 1200px;
    margin: 0 auto;
    &__filter {
      background: #fff;
      padding: 1.2rem 1.6rem;
      font-size: 0.9rem;
      .divider {
        height: 2px;
        width: 100%;
        margin: 0.5rem 0;
        background: #f9f9f9;
      }
      > ul {
        display: flex;
        .title {
          color: #999;
        }
        .item {
          margin-left: 20px;
          cursor: pointer;
          &.active {
            color: rgb(255, 48, 48);
          }
        }
      }
    }

    &__list {
      padding: 40px 0;
    }
  }
}
</style>
